﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VirtualPedal;

namespace VPApp
{
	/// <summary>
	/// Interaction logic for InputDevicePanel.xaml
	/// </summary>
	public partial class AudioDevicePanel : UserControl
	{
		public enum EDeviceType
		{
			None,
			Capture,
			Render,
		}

		EDeviceType m_DeviceType = EDeviceType.None;
		AudioDeviceBase m_Device = null;
		bool m_bIsInputDeviceOpen = false;

		public AudioDeviceBase Device { get { return m_Device; } }

		public event EventHandler<EventArgs> DeviceStateChanged;

		public AudioDevicePanel()
		{
			InitializeComponent();
		}

		public AudioDevicePanel(EDeviceType type)
		{
			InitializeComponent();
			m_DeviceType = type;
			if (m_DeviceType == EDeviceType.Capture)
			{
				groupBox1.Header = "Input Device";
				m_Device = new AudioSource();
			}
			else if (m_DeviceType == EDeviceType.Render)
			{
				groupBox1.Header = "Output Device";
				m_Device = new AudioRenderer();
				
			}
			else
			{
				return;
			}

			m_Device.Init();
			foreach (var item in m_Device.Devices)
			{
				comboBoxInputDevices.Items.Add(item);
			}
			if (comboBoxInputDevices.Items.Count > 0)
			{
				comboBoxInputDevices.SelectedIndex = 0;
			}
		}

		void OnDeviceStateChanged()
		{
			if (m_bIsInputDeviceOpen)
			{
				buttonConnect.Content = "Disconnect";
				comboBoxInputDevices.IsEnabled = false;
			}
			else
			{
				if (comboBoxInputDevices.SelectedIndex >= 0)
				{
					buttonConnect.Content = "Connect";
				}
				comboBoxInputDevices.IsEnabled = true;
			}
			if (DeviceStateChanged != null)
			{
				DeviceStateChanged(this, null);
			}
		}

		void OpenDevice()
		{
			m_Device.Open(comboBoxInputDevices.SelectedItem as DeviceDescriptor);
			m_bIsInputDeviceOpen = m_Device.IsOpened;
			OnDeviceStateChanged();
		}

		void CloseDevice()
		{
			m_Device.Close();
			m_bIsInputDeviceOpen = false;
			OnDeviceStateChanged();
		}

		private void comboBoxInputDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			buttonConnect.IsEnabled = true;
		}

		private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			textBoxVolumn.Text = string.Format("{0}{1:f2} db", e.NewValue < 0 ? "" : "+", e.NewValue);
			m_Device.Gain = (float)e.NewValue;
		}

		private void buttonConnect_Click(object sender, RoutedEventArgs e)
		{
			if (!m_bIsInputDeviceOpen)
			{
				OpenDevice();
			}
			else
			{
				CloseDevice();
			}
		}

		private void UserControl_Unloaded(object sender, RoutedEventArgs e)
		{
			if (m_bIsInputDeviceOpen)
			{
				m_Device.Close();
				m_bIsInputDeviceOpen = false;
			}
		}
	}
}